import {Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
import {fadeIn} from "../../animate";
import {UtilService} from "../../my-service/util.service";
import {NzModalService, NzMessageService} from 'ng-zorro-antd';
import {TeamManagerService} from "../../my-service/teamManager.service";

declare let $: any;

@Component({
  selector: 'app-modify-duplicate',
  templateUrl: './modify-duplicate.component.html',
  styleUrls: ['./modify-duplicate.component.css'],
  animations: [fadeIn]
})
export class ModifyDuplicateComponent implements OnInit {
  @Input() duplicate: any;
  @Output() onDuplicate = new EventEmitter();
  readImgUrl = '';
  defaultImg ='';
  IsUploadImage = false;
  firstUrl: string = null;//最初的图片地址，只有用户点击确认并且上传了新的封面图片才删除掉这个图片
  constructor(private utilServe: UtilService,
              private message: NzMessageService,
              private teamServe: TeamManagerService,) {
    this.readImgUrl = this.utilServe.readImgUrl;
    this.defaultImg = this.utilServe.defaultImg;
  }

//删除图片
  deleteImg(url) {
    this.teamServe.deleteIMG(url).then(res => {
      if (res.code !== 1) {
        console.log(res);
      }
    })
  }

  ngOnInit() {
    this.firstUrl = this.duplicate.cover;

  }

  //上传教案包副本图片
  UploadCover($event) {
    const fileReader = new FileReader();
    const file = $event.target.files[0];
    if (file.type !== 'image/jpeg' && file.type !== 'image/png') {
      this.message.error('封面上传失败，封面类型应该为jpeg或者png');
      return;
    }
    if (file.size > 2097152) {
      this.message.error('封面上传失败，封面图片大小请限制在2M以内');
      return;
    }
    let that = this;
    fileReader.onload = function ($event) {
      that.IsUploadImage = true;
      $('.UploadForm').ajaxSubmit({
        url: that.utilServe.loadImgUrl + `/?directory=LessonPlan/Template/User&userGuid=${that.duplicate.teamGuid}`,
        type: 'post',
        success: function (data) {
          if (data) {
            if (that.duplicate.cover !== that.firstUrl) {
              //删除前面上传且不是初始图片
              that.deleteImg(that.duplicate.cover);
            }
            that.IsUploadImage = false;
            that.duplicate.cover = data;
            that.message.success('教案包封面上传成功');
          }
        },
        error: function () {
          that.IsUploadImage = false;
          that.message.error('图片上传失败，请重试');
        }
      });
    };
    fileReader.readAsDataURL(file);
  }

//  点击关闭按钮进行关闭的
  onClose() {
    if (this.duplicate.cover !== this.firstUrl) {
      //用户非常操作，删除非原始图片
      this.deleteImg(this.duplicate.cover);
      this.duplicate.cover = this.firstUrl;
    }
    this.duplicate.isModify = false;
  }

  //点击确认
  confirm() {
    let errMsg = '';
    if (this.duplicate.name.trim() === '') {
      errMsg = '教案包标题不能为空';
    } else if (this.duplicate.enName.trim() === '') {
      errMsg = '教案包副标题不能为空';
    } else if (this.duplicate.publisher.trim() === '') {
      errMsg = '教案包出品方不能为空';
    }
    if (errMsg !== '') {
      this.message.error(errMsg);
      console.log(1);
      return;
    } else {
      if (this.duplicate.cover !== this.firstUrl && this.firstUrl.indexOf('LessonPlan/Template/User/') !== -1) {
        //如果用户的原始图片不是系统图片就给删了
        this.deleteImg(this.firstUrl);
      }
      this.teamServe.modifyDuplicate(this.duplicate).then(res => {
        if (res.code == 1) {
          this.message.success('修改教案包成功');
          this.duplicate.isModify = false;
          this.onDuplicate.emit(this.duplicate);
        } else {
          this.message.success('修改教案包失败，请稍后再试')
        }
      });
    }
  }
}
